@echo off
setlocal enabledelayedexpansion

echo ==========================================
echo Sokit CLI Linux WSL Build Script
echo ==========================================

echo.
echo This script will build Linux version of sokit-cli using WSL
echo.

REM Check WSL
wsl --version >nul 2>&1
if %errorlevel% neq 0 (
    echo Error: WSL not found
    echo Please install WSL first: wsl --install
    pause
    exit /b 1
)

echo WSL check passed!
echo.

REM Select Linux distribution
echo Please select WSL distribution:
echo 1) Ubuntu (default)
echo 2) Debian
echo 3) Other

set /p distro_choice="Enter your choice (1-3): "

REM Select target architecture
echo.
echo Please select target architecture:
echo 1) x86_64 (64-bit)
echo 2) x86 (32-bit)
echo 3) ARM64
echo 4) ARM32

set /p arch_choice="Enter architecture choice (1-4): "

if "%distro_choice%"=="1" (
    set WSL_DISTRO=Ubuntu
) else if "%distro_choice%"=="2" (
    set WSL_DISTRO=Debian
) else if "%distro_choice%"=="3" (
    set /p WSL_DISTRO="Enter distribution name: "
) else (
    echo Invalid choice, using default Ubuntu
    set WSL_DISTRO=Ubuntu
)

REM Set target architecture
if "%arch_choice%"=="1" (
    set TARGET_ARCH=x86_64
    set CMAKE_ARCH=x86_64
) else if "%arch_choice%"=="2" (
    set TARGET_ARCH=i686
    set CMAKE_ARCH=i686
) else if "%arch_choice%"=="3" (
    set TARGET_ARCH=aarch64
    set CMAKE_ARCH=aarch64
) else if "%arch_choice%"=="4" (
    set TARGET_ARCH=arm
    set CMAKE_ARCH=arm
) else (
    echo Invalid architecture choice, using default x86_64
    set TARGET_ARCH=x86_64
    set CMAKE_ARCH=x86_64
)

echo Selected distribution: %WSL_DISTRO%
echo Selected architecture: %TARGET_ARCH%
echo.

REM Check if distribution is installed
echo Checking WSL distribution...
wsl -l -v | findstr "%WSL_DISTRO%" >nul
if %errorlevel% neq 0 (
    echo Error: Distribution %WSL_DISTRO% not found
    echo Please install first: wsl --install -d %WSL_DISTRO%
    pause
    exit /b 1
)

echo Distribution check passed!
echo.

REM Create build script
echo Creating WSL build script...

(
echo #!/bin/bash
echo set -e
echo.
echo echo "=========================================="
echo echo "WSL Linux 构建脚本"
echo echo "=========================================="
echo.
echo # 更新系统
echo echo "更新系统包..."
echo sudo apt update
echo.
echo # 安装依赖
echo echo "安装构建依赖..."
echo sudo apt install -y cmake g++ make git qt6-base-dev qt6-tools-dev
echo.
echo # 进入项目目录
echo echo "进入项目目录..."
echo cd /mnt/c/Users/%USERNAME%/Projects/SVN/gitee/sokit
echo.
echo # 创建构建目录
echo echo "创建构建目录..."
echo mkdir -p build-cli
echo cd build-cli
echo.
echo # 配置CMake
echo echo "配置CMake..."
echo cmake ../src/sokit-cli -DCMAKE_BUILD_TYPE=Release -DCMAKE_SYSTEM_PROCESSOR=%CMAKE_ARCH%
echo.
echo # 编译
echo echo "编译项目..."
echo make -j$(nproc)
echo.
echo # 创建发布包
echo echo "创建发布包..."
echo mkdir -p sokit-cli-linux-%TARGET_ARCH%
echo cp sokit-cli sokit-cli-linux-%TARGET_ARCH%/
echo cp ../README.md sokit-cli-linux-%TARGET_ARCH%/ 2>/dev/null || true
echo cp ../LINUX_BUILD_GUIDE.md sokit-cli-linux-%TARGET_ARCH%/ 2>/dev/null || true
echo.
echo # 创建tar.gz包
echo echo "打包..."
echo tar -czf sokit-cli-linux-%TARGET_ARCH%-$(date +%%Y%%m%%d).tar.gz sokit-cli-linux-%TARGET_ARCH%/
echo.
echo echo "=========================================="
echo echo "构建完成!"
echo echo "=========================================="
echo echo "可执行文件: build-cli/sokit-cli"
echo echo "发布包: build-cli/sokit-cli-linux-%TARGET_ARCH%-*.tar.gz"
echo echo "=========================================="
) > wsl_build.sh

echo Build script created: wsl_build.sh
echo.

REM Run WSL build
echo Starting WSL build...
echo Note: First run may take longer to install dependencies
echo.

wsl -d %WSL_DISTRO% bash wsl_build.sh

if %errorlevel% equ 0 (
    echo.
    echo ==========================================
    echo WSL build completed!
    echo ==========================================
    echo Build results located at: build-cli/
    echo.
    if exist "build-cli" (
        echo Generated files:
        dir build-cli
    )
) else (
    echo.
    echo WSL build failed, please check error messages
)

REM Clean up temporary files
del wsl_build.sh

pause
exit /b 0 